// pages/home/home.js
const myRequest = require('../../assets/js/request.js');
const api = require('../../assets/js/api.js')
let arr = []
let carlists=[]
Page({

  /**
   * 页面的初始数据
   */
  data: {
    // imgUrls: ['../../assets/images/swiper1.png', '../../assets/images/swiper2.png' ],
    imgUrls: ['https://oneplusminiapp-1300362285.cos.ap-shanghai.myqcloud.com/swiper1.png', 'https://oneplusminiapp-1300362285.cos.ap-shanghai.myqcloud.com/swiper2.png'],

    indicatorDots: true,
    autoplay: true,
    interval: 5000,
    duration: 1000,
    Hei: "",
    wid: "",
    activeIndex: '',
    navData: [{
        text: '商品精选'
      },
      {
        text: '耳机'
      },
      {
        text: '壳/膜'
      },
      {
        text: '适配器/数据线'
      },
      {
        text: '套装'
      },
      {
        text: '生活馆'
      }
    ],
    currentTab: 0,
    navScrollLeft: 0,

    phoneid: [1001, 1002],
    swiperBHeight: 460, //解决swiper-item内容被隐藏显示不全的问题
    datalist: [],
    lowprice: 1,
    type: '商品精选'
  },
  changeIndicatorDots: function(e) {
    this.setData({
      indicatorDots: !this.data.indicatorDots
    })
  },
  changeAutoplay: function(e) {
    this.setData({
      autoplay: !this.data.autoplay
    })
  },
  intervalChange: function(e) {
    this.setData({
      interval: e.detail.value
    })
  },
  durationChange: function(e) {
    this.setData({
      duration: e.detail.value
    })
  },
  imgH: function(e) {
    var winWid = wx.getSystemInfoSync().windowWidth;
    var imgh = e.detail.height;　　　　　　　　　　　　　　　
    var imgw = e.detail.width;
    var swiperH = winWid * imgh / imgw + "px"
    var swiperW = winWid　　
    console.log(swiperW)　　　　　　　
    this.setData({
      Hei: swiperH,
      wid: swiperW　　　　　　
    })
  },

  // getreq: function (e) {
  //   arr = []
  //   console.log(e)
  //   this.setData({
  //     activeIndex: e.target.dataset.index,
  //     kind: e.target.dataset.kind,
  //     pageIndex: 1
  //   })
  // },


  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    let that = this.data.type
    wx.getSystemInfo({
      success: (res) => {
        this.setData({
          pixelRatio: res.pixelRatio,
          windowHeight: res.windowHeight,
          windowWidth: res.windowWidth
        })
      },
    })
    this.showdata(that)
    wx.setStorage({
      key: 'shopsnum',
      data: arr,
    })
    wx.setStorage({
      key: 'carlists',
      data: carlists,
    })
  },
  //渲染数据
  showdata: function(data) {
    let that = this
    myRequest._get(api.goodsList + '?type=' + data).then((res) => {
      that.setData({
        datalist: res.data.goodslist,
        lowprice: 999,
        swiperBHeight: 500 * Math.ceil(res.data.goodslist.length / 2)
      })
    })
  },
  //点击查看详情
  godetail: function(e) {
    let myid = e.currentTarget.dataset.id
    console.log(myid)
    myRequest._get(api.goodsList + '?id=' + myid).then((res) => {
      console.log(res.data)
      wx.setStorage({
        key: 'detailItem',
        data: res.data.goodslist,
      })
      wx.navigateTo({
        url: '/pages/detail/detail',
      })
    })
  },
  switchNav(event) {
    this.setData({
      type: event.currentTarget.dataset.type
    })
    this.showdata(this.data.type)
    var cur = event.currentTarget.dataset.current;
    //每个tab选项宽度占1/5
    var singleNavWidth = this.data.windowWidth/5;
    //tab选项居中                            
    this.setData({
      navScrollLeft: (cur -2)* singleNavWidth
    })
    if (this.data.currentTab == cur) {
      return false;
    } else {
      this.setData({
        currentTab: cur
      })
    }
  },
  switchTab(event) {
    var cur = event.detail.current;
    var singleNavWidth = this.data.windowWidth;
    this.showdata(this.data.navData[cur].text)
    this.setData({
      currentTab: cur,
      navScrollLeft: (cur - 2) * singleNavWidth
    });
  },
  //领券中心
  gocoupcenter:function(){
    wx.navigateTo({
      url: '/pages/conponscenter/conponscenter',
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  }
})